package com.stedu.controller;

import com.stedu.bean.Department;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @PostMapping
    public RespBean insert(@Validated @RequestBody Department department) throws SteduException {
        //添加
        if (departmentService.insert(department)) {
             return RespBean.ok("添加成功~~~");
        } else {
            return RespBean.error("添加失败，请重新添加");
        }
    }

    @DeleteMapping("/{did}")
    public RespBean delete(@PathVariable("did") Integer did) throws SteduException {
        if (departmentService.delete(did)) {
            return RespBean.ok("删除成功~~~");
        } else {
            return RespBean.error("删除失败，请重新删除");
        }
    }

    @PutMapping
    public RespBean update(@Validated @RequestBody Department department) throws SteduException {
        if (departmentService.update(department)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败，请重新修改");
        }
    }


    @GetMapping("/{did}")
    public RespBean selectByDid(@PathVariable("did") Integer did) {
        Department d = departmentService.selectByDid(did);
        return RespBean.ok("", d);
    }

    @GetMapping
    public RespBean selectAll()  {
        List<Department> departmentList = departmentService.selectAll();
        return RespBean.ok("", departmentList);
    }
}
